package cn.yaboya.org.service.impl;

import cn.yaboya.basic.service.Impl.BaseServiceImpl;
import cn.yaboya.basic.utils.MD5Utils;
import cn.yaboya.basic.utils.StrUtils;
import cn.yaboya.org.domain.Employee;
import cn.yaboya.org.mapper.EmployeeMapper;
import cn.yaboya.org.service.IEmployeeService;
import cn.yaboya.user.domain.Logininfo;
import cn.yaboya.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;



    /**
    * @Title: initEmployee
    * @Description: 初始化员工对象
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/7/6 12:01
    * @Parameters: [employee]
    * @Return void
    */
    private void initEmployee(Employee employee){
        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        // 设置盐值密码
        String md5Pwd = MD5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5Pwd);
    }

    /**
    * @Title: employeeToLogininfo
    * @Description: 将员工信息copy到日志表中
    * @Author: LT
    * @Version: 1.0
    * @Date:  2022/7/6 15:11
    * @Parameters: [employee]
    * @Return cn.yaboya.user.domain.Logininfo
    */
    private Logininfo employeeToLogininfo(Employee employee){
        // 1.拷贝数据
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        // 2 设置用户类型为员工
        logininfo.setType(0);
        return logininfo;
    }

    @Override
    @Transactional
    public void add(Employee employee) {
        // 因为传过来的是明文密码 加密员工密码
        initEmployee(employee);
        // 将employee信息保存到logininfo中
        Logininfo logininfo = employeeToLogininfo(employee);
        // 保存loginingfo数据
        logininfoMapper.add(logininfo);
        //设置员工表单外键
        employee.setLogininfoId(logininfo.getId());
        // 保存
        employeeMapper.add(employee);
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        // 因为传过来的是明文密码 加密员工密码
        initEmployee(employee);
        // 将employee信息保存到logininfo中
        Long logininfoId = employee.getLogininfoId();
        Logininfo logininfo = employeeToLogininfo(employee);
        logininfo.setId(logininfoId);
        // 保存loginingfo数据
        logininfoMapper.update(logininfo);
        //设置员工表单外键
        employee.setLogininfoId(logininfo.getId());
        // 保存
        employeeMapper.update(employee);
        super.update(employee);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        Employee employee = employeeMapper.loadById(id);
        logininfoMapper.remove(employee.getLogininfoId());
        employeeMapper.remove(id);

    }


    @Override
    public Employee loadByRepetition(Employee employee) {
        return employeeMapper.loadByRepetition(employee);
    }

}
